package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Details;
import cn.wolfcode.crm.domain.Directory;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDetailsService;
import cn.wolfcode.crm.service.IDirectoryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("details")
public class DetailsController {
    /*details/view.do*/
    @Autowired
    private IDirectoryService directoryService;
    @Autowired
    private IDetailsService detailsService;

    @Autowired
    @RequestMapping("view")
    public String index() {
        return "details";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"details:query", "字典列表"}, logical = Logical.OR)
    public PageResult query(QueryObject qo) {
        return detailsService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"details:saveOrUpdate", "字典明细新增/更改"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Details dept) {
        Directory sn = directoryService.selectByPrimaryKey(dept.getDirectory().getId());
        try {
            detailsService.insert(dept, sn);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("selectDetailsByid")
    @ResponseBody
    public Details selectDetailsByid(Long id) {
        return detailsService.selectDetailsByid(id);
    }
}
